import{R as z,U as A,am as W,T as q,l as B,k as _,p as L,bf as G,j as K}from"./CtbHscGXagutd.js";import{e as o,G as E,K as V}from"./CtybWkkwagutd.js";var m,F=(m=class extends z{_totalUnits;_completedUnits;_isCancelled=!1;_resetWhenFinished=!0;_children=[];dispose=A();name;constructor(e={}){super(),this._totalUnits=e.totalUnits??0,this._completedUnits=e.completeUnits??0,this._resetWhenFinished=e.resetWhenFinished??!0,this.name=e.name??E("progress"),this.dispose.add(async()=>{for(let i of this._children)await i.dispose();await this.emit("progressDispose",this)})}update(){this.emit("progressChanged",this),this._isCancelled&&this._resetWhenFinished&&this.getTotalUnits()<=this.getCompletedUnits()&&this.reset()}reset(){if(this._isCancelled){this._isCancelled=!1;for(let e of this._children)e.reset();this.update()}}async cancel(){if(!this._isCancelled){this._isCancelled=!0,await this.emit("progressCancelled",this);for(let e of this._children)await e.cancel();this.update()}}addChild(e){e.on("progressDispose",()=>this.removeChild(e)),e.on("progressChanged",()=>this.update()),this._children.includes(e)||this._children.push(e),this.update()}createChildProgress(e){let i=new m(e);return this.addChild(i),i}removeChild(e){W(this._children,e),this.update()}getTotalUnits(){if(this.isIndeterminate())return 0;let e=this._totalUnits;for(let i of this._children)e+=i.getTotalUnits();return e}getCompletedUnits(){if(this.isIndeterminate())return 0;let e=this._completedUnits;for(let i of this._children)e+=i.getCompletedUnits();return e}isIndeterminate(){return this._totalUnits<=0&&this._children.length<=0}isCancelled(){return this._isCancelled}isFinished(){return this.dispose.isDisposed()||!this.isIndeterminate()&&this.getTotalUnits()<=this.getCompletedUnits()}getFraction(){if(this.isIndeterminate())return 0;let e=this.getCompletedUnits()/this.getTotalUnits();return Number.isNaN(e)&&(e=0),Math.min(1,Math.max(0,e))}getChildrenCount(){return this._children.length}setTotalUnits(e,i){this._totalUnits=e,i!=null&&(this._completedUnits=i),this.update()}incTotalUnits(e=1){this._totalUnits+=e,this.update()}setCompletetedUnits(e){this._completedUnits=e,this.update()}setCompleted(){this._completedUnits=this._totalUnits,this.update()}incCompletedUnits(e=1){this._completedUnits+=e,this.update()}toString(e=0){let i=`${"  ".repeat(e)}${this.name}: ${this._completedUnits} of ${this._totalUnits} units, ${Math.floor(this.getFraction()*100)} %, cancel=${this._isCancelled}
`;for(let U of this._children)i+=U.toString(e+1);return e===0?i.trim():i}},o(m,"Progress"),m);function S(h={}){let{maxParallel:e=3,name:i=E("pool"),idConflictResolution:U="memoize"}=h,c=new z,v=A();v.add(k);let w=new F({name:i});w.on("progressCancelled",k);let f=0,C=0,x=0,b=0,a={};async function P(){if(f>0){let[n,t]=q();return c.once("didFinish",t),n}}o(P,"waitFinishAll");async function j(){for(let n of Object.keys(a))a[n].state==="finished"&&(await a[n].progress.dispose(),delete a[n])}o(j,"cleanupFinished");function I(){c.emit("didFinish"),f=0,C=0,j(),w.reset()}o(I,"didFinish");function g(){let n=0,t=0;for(let{max:l,resolved:u,state:s}of Object.values(a))n+=l,t+=s==="finished"?l:Math.min(l,u);c.emit("didUpdate",f,C,n,t)}o(g,"didUpdate");function T(){if(g(),f>0&&f===C&&I(),x>=e)return;let n=Object.values(a).filter(t=>t.state==="waiting");if(n.length>0){let t;for(let l of n)l.group!=null&&Object.values(a).some(u=>u.state==="running"&&u.id!==l.id&&u.group===l.group)||(t==null||l.priority<t.priority)&&(t=l);if(t!=null){let l=t.id,u=t.done;t.state="running",++x,c.emit("didStart",l);let s=o(d=>{t&&(t.result=d,t.state="finished",t.resolved=t.max,t.progress?.setCompleted()),--x,++C,T()},"taskFinished");t.task(t).then(d=>{u(d),c.emit("didResolve",l,d),s(d)}).catch(d=>{u(),c.emit("didReject",l,d),s()})}}}o(T,"performNext");function p(n){let t=a[n];t&&t.state==="waiting"&&(a[n].state="finished",++C,c.emit("didCancel",n),a[n].progress.dispose(),g())}o(p,"cancel");function k(){Object.keys(a).forEach(p)}o(k,"cancelAll");function R(n){return a[n]!=null}o(R,"hasById");function M(n,t={}){let l,u=new Promise(r=>l=r),s=t.id??V();if(a[s]!=null){let r=t.idConflictResolution??U;if(r==="replace")p(s);else if(r==="memoize"||r==="prioritize"){let N=a[s];return r==="prioritize"&&(N.priority=++b),{id:s,promise:(async()=>{if(N.state==="finished")return a[s].result})(),dispose:o(()=>p(s),"dispose"),cancel:o(()=>p(s),"cancel")}}else throw new Error(`Pool task with id=${s} already exists!`)}let d=new F({name:s,totalUnits:t.max??1,completeUnits:t.resolved??0});return w.addChild(d),a[s]={id:s,task:n,priority:++b,group:t.group,state:"waiting",max:t.max??1,resolved:t.resolved??0,done:l,payload:t.payload,progress:d,setMax(r){d.setTotalUnits(r),a[s].max=r,g()},setResolved(r){d.setCompletetedUnits(r),a[s].resolved=r,g()},incResolved(r=1){d.incCompletedUnits(r),a[s].resolved+=r,g()}},++f,T(),{id:s,promise:u,dispose:o(()=>p(s),"dispose"),cancel:o(()=>p(s),"cancel")}}return o(M,"enqueue"),{events:c,cancel:p,cancelAll:k,hasById:R,progress:w,enqueue:M,dispose:v,waitFinishAll:P}}o(S,"usePool");B([]);const H=L("tasks"),y=new F({name:"rootProgress",totalUnits:0,completeUnits:0});function J(h){const e=S(h);return y.addChild(e.progress),e}const Q=_(""),O=_(0),$=_(0);_(0);const D=_(!1),X=G(()=>{O.value=y.getTotalUnits(),$.value=y.getCompletedUnits(),D.value=O.value!==$.value},{delay:200});y.on("progressChanged",X);const Y=J({name:"tasks",maxParallel:5});function Z(h,e,i,U){return Y.enqueue(async c=>{try{Q.value=h??"",h&&void 0,c&&await e(c)}catch(v){H.error("task error =",v)}},{payload:i,group:U})}function se(h,e,i){return Z(h,e,i,"serial")}globalThis.isNodeTestEnv||window.addEventListener("beforeunload",h=>{if(D.value)return h.preventDefault(),h.returnValue=K("tasks.beforeUnload")},{capture:!0});export{Z as a,O as b,D as c,Q as d,se as e,J as f,y as p,$ as t};